package art.server;

import java.util.ArrayList;
import java.util.List;

import art.server.db.entities.Art;
import art.server.db.entities.Genre;
import art.server.db.entities.Rating;
import art.server.db.entities.PortalUser;
import art.server.db.entities.UserLevel;
import art.shared.entities.ArtShared;
import art.shared.entities.GenreShared;
import art.shared.entities.RatingShared;
import art.shared.entities.UserLevelShared;
import art.shared.entities.UserShared;

public class EntityConverter {

	private static EntityConverter converter = null;

	protected EntityConverter() {

	}

	public static EntityConverter getConverter() {
		if (converter == null) {
			converter = new EntityConverter();
		}
		return converter;
	}

	public ArtShared convertArtToShared(Art art) {
		ArtShared as = new ArtShared();
		as.setCreatorId(art.getCreatorId());
		as.setGenre(this.convertGenreToShared(art.getGenre()));
		as.setId(art.getId());
		List<RatingShared> ratings = new ArrayList<RatingShared>();
		for (Rating rating : art.getRatings()) {
			ratings.add(this.convertRatingToShared(rating));
		}
		as.setRatings(ratings);
		as.setTitle(art.getTitle());
		as.setUploadedOn(art.getUploadedOn());
		return as;
	}

	public Art convertArt(ArtShared art) {
		Art a = new Art();
		a.setCreatorId(art.getCreatorId());
		a.setId(art.getId());
		a.setGenre(this.convertGenre(art.getGenre()));
		List<Rating> ratings = new ArrayList<Rating>();
		for (RatingShared rating : art.getRatings()) {
			ratings.add(this.convertRating(rating));
		}
		a.setRatings(ratings);
		a.setTitle(art.getTitle());
		a.setUploadedOn(art.getUploadedOn());
		return a;
	}

	public RatingShared convertRatingToShared(Rating rating) {
		RatingShared rs = new RatingShared();
		rs.setCreatedOn(rating.getCreatedOn());
		rs.setCreatorId(rating.getCreatorId());
		rs.setId(rating.getId());
		rs.setRate(rating.getRate());
		rs.setArtId(rating.getArtId());
		rs.setReview(rating.getReview());
		return rs;
	}

	public Rating convertRating(RatingShared rating) {
		Rating r = new Rating();
		r.setCreatedOn(rating.getCreatedOn());
		r.setCreatorId(rating.getCreatorId());
		r.setId(rating.getId());
		r.setArtId(rating.getArtId());
		r.setRate(rating.getRate());
		r.setReview(rating.getReview());
		return r;
	}

	public UserShared convertUserToShared(PortalUser user) {
		UserShared us = new UserShared();
		List<ArtShared> ass = new ArrayList<ArtShared>();
		for (Art art : user.getArts()) {
			ass.add(this.convertArtToShared(art));
		}
		switch (user.getLevel()) {
		case VISITOR:
			us.setLevel(UserLevelShared.VISITOR);
			break;
		case NORMAL:
			us.setLevel(UserLevelShared.NORMAL);
			break;
		case ADMIN:
			us.setLevel(UserLevelShared.ADMIN);
			break;
		default:
			break;
		}
		us.setArts(ass);
		us.setDateOfBirth(user.getDateOfBirth());
		us.setEmail(user.getEmail());
		us.setGender(user.getGender());
		us.setId(user.getId());
		us.setName(user.getName());
		us.setNickname(user.getNickname());
		return us;
	}

	public PortalUser convertUser(UserShared user) {
		PortalUser u = new PortalUser();
		List<Art> as = new ArrayList<Art>();
		for (ArtShared art : user.getArts()) {
			as.add(this.convertArt(art));
		}
		switch (user.getLevel()) {
		case VISITOR:
			u.setLevel(UserLevel.VISITOR);
			break;
		case NORMAL:
			u.setLevel(UserLevel.NORMAL);
			break;
		case ADMIN:
			u.setLevel(UserLevel.ADMIN);
			break;
		default:
			break;
		}
		u.setArts(as);
		u.setDateOfBirth(user.getDateOfBirth());
		u.setEmail(user.getEmail());
		u.setGender(user.getGender());
		u.setId(user.getId());
		u.setName(user.getName());
		u.setNickname(user.getNickname());
		return u;
	}

	public GenreShared convertGenreToShared(Genre genre) {
		GenreShared gs = new GenreShared();
		gs.setName(genre.getName());
		gs.setId(genre.getId());
		gs.setParentId(genre.getParentId());
		return gs;
	}

	public Genre convertGenre(GenreShared genre) {
		Genre g = new Genre();
		g.setName(genre.getName());
		g.setId(genre.getId());
		g.setParentId(genre.getParentId());
		return g;
	}
}